<?php

class CAdvertLog {
	public static function CheckForLog() {
		if ($partner = request('advfrom')) {
			switch ($partner) {
				case 'vne':
					$type = request('advtype');
					$cat = request('advcat');
					$time = strtotime($timestr = date('Y-m-d H:0:0'));
					if (!($product_id = request('id'))) {
						$product_id = 0;
					}
					$cache_name = 'vne-'.$type.'-'.$cat.'-'.$product_id;
					if (!($data = CSystem::$cachesession->get($cache_name))) {
						$log = CTable('vne_report')->select(EAnd(
							EEqual('rpt_type', $type),
							EEqual('rpt_cat', $cat),
							EEqual('rpt_id', $product_id),
							EEqual('rpt_time', $timestr)
						));
						if ($log) {
							$data = array(
								'time' => $time,
								'count' => $log['rpt_count']
							);
						} else {
							$data = array(
								'time' => $time,
								'count' => 0
							);
						}
					}
					$data['count']++;

					if ($time != $data['time']) {
						$count = $data['count'];
						$data['count'] = 0;
						$data['time'] = strtotime($timestr = date('Y-m-d H:0:0'));
						CSystem::$cachesession->set($cache_name, $data, 86400);

						CTable('vne_report')->save( array(
							'rpt_type'		=> $type,
							'rpt_cat'			=> $cat,
							'rpt_id'			=> $product_id,
							'rpt_time'		=> $timestr,
							'rpt_count'		=> $count
						));

						CTable('vne_report')->update(
							array('rpt_count'=>$count),
							array(
								'rpt_type'		=> $type,
								'rpt_cat'			=> $cat,
								'rpt_id'			=> $product_id,
								'rpt_time'		=> $timestr,
						));
						return;
					}

					CSystem::$cachesession->set($cache_name, $data, 86400);
					break;
			}
		}
	}
}